package com.ys.config.database;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration
@MapperScan(
		// 指定该数据源扫描指定包下面的Mapper接口文件
        basePackages = "com.ys.mapper.business",
        sqlSessionFactoryRef = "sqlSessionFactoryPrimary",
        sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
public class DataSourcePrimaryConfig {

    // mapper扫描xml文件的路径
    private static final String MAPPER_LOCATION = "classpath:mapper/*/*.xml";
	
	// 注入数据源1
    @javax.annotation.Resource
    private DataSource primaryDataSource;

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(primaryDataSource);
        
        /*
         * 文件夹下的时候,需要手动指定.xml文件所在的路径
         */ 
//        org.springframework.core.io.Resource[] mappers = new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION);
//        sqlSessionFactoryBean.setMapperLocations(mappers);
        
//        List<org.springframework.core.io.Resource> resources = new ArrayList<org.springframework.core.io.Resource>();
//      {
//      org.springframework.core.io.Resource[] mappers = new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION);
//      resources.addAll(Arrays.asList(mappers));
//  }
//  {
//      org.springframework.core.io.Resource[] mappers = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sys/*.xml");
//      
//      resources.addAll(Arrays.asList(mappers));
//  }
//        sqlSessionFactoryBean.setMapperLocations(resources.toArray(new Resource[resources.size()]));
        sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
        return sqlSessionFactoryBean.getObject();
    }
    
    public Resource[] resolveMapperLocations() {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath*:mapper/business/*.xml");
        mapperLocations.add("classpath*:mapper/sys/*.xml");
        List<Resource> resources = new ArrayList<Resource>();
        for (String mapperLocation : mapperLocations) {
            try {
                Resource[] mappers = resourceResolver.getResources(mapperLocation);
                resources.addAll(Arrays.asList(mappers));
            } catch (IOException e) {
            	e.printStackTrace();
            }
        }

        return resources.toArray(new Resource[resources.size()]);
    }

    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryPrimary());
    }
}
